<?php

namespace app\service;

class DesEncrypt
{
	//密钥
	private $secretKey;
	/*
	 * 构造方法
	 */
	public function __construct($secretKey = '')
	{

		$this->secretKey = $secretKey;

		$this->pkcSecretKey();
	}
	/**
	 * 加密
	 */
	public function encrypt($data)
	{
		$data = mb_convert_encoding($data, 'UTF-8');
		$encry = openssl_encrypt($data, 'DES-ECB', $this->secretKey, OPENSSL_RAW_DATA);
		return base64_encode($encry);
	}
	/**
	 * 解密
	 */
	public function decrypt($data)
	{
		$decry = openssl_decrypt(base64_decode($data), 'DES-ECB', $this->secretKey, OPENSSL_RAW_DATA);
		return $decry;
	}
	/**
	 * 填充密钥
	 */
	private function pkcSecretKey()
	{
		$key = explode(',', $this->secretKey);
		$str = '';
		foreach ($key as $byte) {
			$str .= chr($byte);
		}
		$key = mb_convert_encoding($str, 'UTF-8');
		$this->secretKey = $key;
	}
}
